/************************************************************************
 * @file AbstractPanelManager.cpp
 * @author Ian Chen
 * @version 1.0
 *
 * @section LICENSE
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 3 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
 * General Public License for more details at
 * http://www.gnu.org/licenses/lgpl.html
 *
 ************************************************************************/
#include <pluginui/manager/AbstractPanelManager.h>
#include <pluginui/manager/spi/HorizontalLayoutPanelContainerFactory.h>
#include <pluginui/manager/spi/SinglePanelContainerFactory.h>
#include <pluginui/manager/spi/TabPanelContainerFactory.h>
#include <pluginui/manager/spi/VerticalLayoutPanelContainerFactory.h>

namespace antu
{
namespace plugin
{
namespace ui
{
AbstractPanelManager::AbstractPanelManager()
{
    this->registerContainerFactory(AbstractPanelManager::PT_None, new SinglePanelContainerFactory);
    this->registerContainerFactory(AbstractPanelManager::PT_Tab, new TabPanelContainerFactory);
    this->registerContainerFactory(AbstractPanelManager::PT_HBox, new HorizontalLayoutPanelContainerFactory);
    this->registerContainerFactory(AbstractPanelManager::PT_VBox, new VerticalLayoutPanelContainerFactory);
}

AbstractPanelManager::~AbstractPanelManager()
{
    map<QString, AbstractPanelContainer*>::iterator iter = this->m_oSortedPanels.begin();
    map<QString, AbstractPanelContainer*>::iterator end = this->m_oSortedPanels.end();

    for (; iter != end; iter++)
    {
        delete iter->second;
    }
    this->m_oPanels.clear();
    this->m_oSortedPanels.clear();

    auto factoryIter = this->m_oContainerFactories.begin();
    auto factoryEnd = this->m_oContainerFactories.end();

    for (; factoryIter != factoryEnd; factoryEnd++)
    {
        delete factoryIter->second;
    }
}

AbstractPanelContainer *AbstractPanelManager::container(const QString &name)
{
    auto iter = this->m_oSortedPanels.find(name);

    if (iter != this->m_oSortedPanels.end())
    {
        return iter->second;
    }
    return nullptr;
}

void AbstractPanelManager::registerContainerFactory(int type, AbstractPanelContainerFactory *pContainerFactory)
{
    Q_ASSERT(pContainerFactory != nullptr);

    auto iter = this->m_oContainerFactories.find(type);

    if (iter != this->m_oContainerFactories.end())
    {
        delete iter->second;
    }
    this->m_oContainerFactories.insert(pair<int, AbstractPanelContainerFactory*>(type, pContainerFactory));
}

AbstractPanelContainer *AbstractPanelManager::container(const QString &name, const QString& title,
                                                        int factoryType,
                                                        Qt::DockWidgetArea area,
                                                        Qt::DockWidgetAreas allowedAreas,
                                                        QDockWidget::DockWidgetFeatures features)
{
    map<QString, AbstractPanelContainer*>::iterator iter = this->m_oSortedPanels.find(name);
    if (iter == this->m_oSortedPanels.end())
    {
        auto factoryIter = this->m_oContainerFactories.find(factoryType);

        if (factoryIter != this->m_oContainerFactories.end())
        {
            AbstractPanelContainer* pItem = factoryIter->second->createContainer(name, title, area, allowedAreas, features);
            this->createContainer(pItem, area, allowedAreas, features);
            this->m_oSortedPanels.insert(pair<QString, AbstractPanelContainer*>(name, pItem));

            this->m_oPanels.push_back(pItem);
            iter = this->m_oSortedPanels.find(name);
        }
        else
        {
            return nullptr;
        }
    }
    return iter->second;
}
}
}
}
